<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <script src="../../js/load.js"></script>
</head>

<body>
<div id="container">
    <pre id="editor" class="ace_editor" style="min-height:670px">
          <s:textarea class="ace_text-input"   cssStyle="width:100%;height:670px;"/>
      </pre>
    <div id="opcontainer">
        <button class="layui-btn" onclick="HandleSave()">保存</button>
        <button class="layui-btn layui-btn-primary" onclick="HandleClose()">关闭</button>
    </div>
</div>
</body>
<script src="../../plugin/editor/ace.js"></script>
<script>
    var editor;
    $(function () {
        LoadDetail();
    })

    function LoadDetail() {
        var param = {
            projectID:GetUrlParam("projectID"),
            fileName:decodeURIComponent(GetUrlParam("fileName"))
        }
        HttpPost(JSON.stringify(param), 'api/config/detail', function (result) {
            if (result.code == 0) {
                initEditor(decodeURIComponent(GetUrlParam("fileName")),result.data.obj)
            }
        })
    }

    function HandleSave() {
        var param = {
            projectID:GetUrlParam("projectID"),
            fileName:decodeURIComponent(GetUrlParam("fileName")),
            fileContent:editor.getValue()
        }
        HttpPost(JSON.stringify(param), 'api/config/update', function (result) {
            if (result.code == 0) {
                CloseDialog();
            }
        })
    }

    function HandleClose() {
        CloseDialog();
    }

    function initEditor(fileName,value){
        editor = ace.edit("editor");
        editor.setTheme("ace/theme/monokai");
        if(fileName.endsWith(".yml")){
            editor.session.setMode("ace/mode/yaml");
        } else if(fileName.endsWith(".xml")){
            editor.session.setMode("ace/mode/xml");
        } else if(fileName.endsWith(".properties")){
            editor.session.setMode("ace/mode/properties");
        }
        editor.setFontSize(15);
        editor.setReadOnly(false);
        editor.setOption("wrap", "free");
        editor.setValue(value,-1);
        ace.require("ace/ext/language_tools");
        editor.setOptions({
            enableBasicAutocompletion: true,
            enableSnippets: true,
            enableLiveAutocompletion: true
        });
    }
</script>

</html>